/*
* EP-Tracing System : a tool to trace mountain hike.           .kNNx
* Copyright (C) 2009  Raffaello Brondi                         'KNNNk
*                                                             ,XNNNl
* This program is free software:                             ,XNN0'
* you can redistribute it and/or modify                     ,XNNx.
* it under the terms of the                                'XNNl
* GNU General Public License                              .KNN:                     ..
* as published by                                        .0NNo                    .ONNO.
* the Free Software Foundation,                          xNNN.  :dkd,            .KNNNN;
* either version 3 of the License, or                   :NNNK ;XNNNNNo          ,KNNNX:
* (at your option) any later version.                   xNNNNcXNNNNNNk         cNNNNx.
*                                                       ,XNNNNNNNNNNX'       .xNNNK;
*                                                        .lKNNNNNNNK'       :KNNNo
*                                                          ;kNNNNNKxl;'.'l0NNNk.
* This program is distributed in                          .'  .:xXNNNNNNNNNNNO,
* the hope that it will be useful,                       :NNXd'   ,oONNNNNNk,
* but WITHOUT ANY WARRANTY;                              xNNNNN0c.    .;;,.
* without even the implied warranty of                   kNNNNNNNXd'
* MERCHANTABILITY or                                     xNNNNNNNNNNOc.
* FITNESS FOR A PARTICULAR PURPOSE.                      xNNNNNNNNNNNNXd.
* See the GNU General Public License                     ONNNN0.,o0NNNNNNl
* for more details.                                      0NNNNd    kNNNNNX.
*                                                        KNNNNl   oNNNNNXc
*                                                       .XNNNN:  dNNNNNx.
* You should have received a copy                       .XNNNN; lNNNNO'
* of the GNU General Public License                     .XNNNN; 'dxo'
* along with this program.                              .XNNNN;
* If not, see <http://www.gnu.org/licenses/>.            0NNNN:
*                                                        kNNNNc
*                                                        oNNNNd
*                                                        ;NNNN0
* Contact: rbrondi@gmail.com                             .XNNNN;
*                                                         kNNNNd
*                                                         .coo:.
* Based on LatZones developed
* by Sami Salkosuo, sami.salkosuo@fi.ibm.com
* http://www.ibm.com/developerworks/java/library/j-coordconvert/index.html
*/

package EP.GPS.CoordinateConversion;

public class LatZones {

    private char[] letters = {'A', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
        'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Z'};
    private int[] degrees = {-90, -84, -72, -64, -56, -48, -40, -32, -24, -16,
        -8, 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 84};
    private char[] negLetters = {'A', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
        'L', 'M'};
    private int[] negDegrees = {-90, -84, -72, -64, -56, -48, -40, -32, -24,
        -16, -8};
    private char[] posLetters = {'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
        'X', 'Z'};
    private int[] posDegrees = {0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 84};
    private int arrayLength = 22;


    public int getLatZoneDegree(String letter) {
        char ltr = letter.charAt(0);
        for (int i = 0; i < arrayLength; i++) {
            if (letters[i] == ltr) {
                return degrees[i];
            }
        }
        return -100;
    }

    public String getLatZone(double latitude) {
        int latIndex = -2;
        int lat = (int) latitude;

        if (lat >= 0) {
            int len = posLetters.length;
            for (int i = 0; i < len; i++) {
                if (lat == posDegrees[i]) {
                    latIndex = i;
                    break;
                }

                if (lat > posDegrees[i]) {
                    continue;
                } else {
                    latIndex = i - 1;
                    break;
                }
            }
        } else {
            int len = negLetters.length;
            for (int i = 0; i < len; i++) {
                if (lat == negDegrees[i]) {
                    latIndex = i;
                    break;
                }

                if (lat < negDegrees[i]) {
                    latIndex = i - 1;
                    break;
                } else {
                    continue;
                }

            }

        }

        if (latIndex == -1) {
            latIndex = 0;
        }
        if (lat >= 0) {
            if (latIndex == -2) {
                latIndex = posLetters.length - 1;
            }
            return String.valueOf(posLetters[latIndex]);
        } else {
            if (latIndex == -2) {
                latIndex = negLetters.length - 1;
            }
            return String.valueOf(negLetters[latIndex]);

        }
    }
}
